/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beliar;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.ImageBuilder;
import de.lessvoid.nifty.controls.Menu;
import de.lessvoid.nifty.controls.MenuItemActivatedEvent;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.elements.render.ImageRenderer;
import de.lessvoid.nifty.render.NiftyImage;
import de.lessvoid.nifty.tools.SizeValue;
import java.util.List;
import org.bushe.swing.event.EventTopicSubscriber;

/**
 *
 * @author andministrator
 */
public class InGameInputs extends AbstractAppState implements ScreenController{

    private AppStateManager stateManager;
    private SimpleApplication app;
    private Nifty nifty;
    private Screen screen;
    private Element menu, menuFirstRow, menuSecondRow, menuThirdRow, firstRowTop, firstRowBottom, 
            secondRowTop, secondRowBottom, thirdRowTop, thirdRowBottom;
    private Element optionsMenu;
    private int adamRooms, kythosRooms, maraRooms; 
    private static final int ADAM = 0;
    private static final int KYTHOS = 1;
    private static final int MARA = 2;
    
    private int menuState;
    private static final int MENU_CLEAR = 0;
    private static final int BUILD_ROOMS = 1;
    private static final int BUILD_ADAM = 2;
    private static final int BUILD_KYTHOS = 3;
    private static final int BUILD_MARA = 4;
    
    private static final int BUILDING_LEVEL_ONE = 1;
    private static final int BUILDING_LEVEL_TWO = 2;
    private static final int BUILDING_LEVEL_THREE = 3;
    
    public InGameInputs(AppStateManager stateManager, Application app){
        System.out.println("InGameInputs");
        this.stateManager = stateManager;
        this.app = (SimpleApplication) app;
    }
    
    @Override
    public void bind(Nifty nifty, Screen screen) {
        System.out.println("bind");
        this.nifty = nifty;
        this.screen = screen;
    }

    @Override
    public void onStartScreen() {
        System.out.println("onStartScreen");
        //createOptionsMenu();
        adamRooms = kythosRooms = maraRooms = 0;
        menu = nifty.getScreen("inGameInputs").findElementByName("menu");
        menuFirstRow = nifty.getScreen("inGameInputs").findElementByName("menuFirstRow");
        menuSecondRow = nifty.getScreen("inGameInputs").findElementByName("menuSecondRow");
        menuThirdRow = nifty.getScreen("inGameInputs").findElementByName("menuThirdRow");
        firstRowTop = nifty.getScreen("inGameInputs").findElementByName("firstRowTop");
        firstRowBottom = nifty.getScreen("inGameInputs").findElementByName("firstRowBottom");
        secondRowTop = nifty.getScreen("inGameInputs").findElementByName("secondRowTop");
        secondRowBottom = nifty.getScreen("inGameInputs").findElementByName("secondRowBottom");
        thirdRowTop = nifty.getScreen("inGameInputs").findElementByName("thirdRowTop");
        thirdRowBottom = nifty.getScreen("inGameInputs").findElementByName("thirdRowBottom");
        menuState = MENU_CLEAR;
        menu.hide();      
    }
    
    @Override
    public void stateAttached(AppStateManager stateManager) {
        
    }
    
    @Override
    public void stateDetached(AppStateManager stateManager){
        
    }

    @Override
    public void setEnabled(boolean enabled){
        super.setEnabled(enabled);
        if(enabled){
            System.out.println("InGameInputs: setEnabled");
            //this.app.getRootNode().attachChild(rootNode);
        }else{
            System.out.println("InGameInputs: setEnabled(false)");
        }
    }
    
    @Override
    public void onEndScreen() {
        System.out.println("onEndScreen");
    }
    
    public void onBuild(){
        System.out.println("onBuild");
        
        if(menu.isVisible()){
            menu.hide();
            clearMenu();
        } else{
            menu.show();
            setupBuildIcons();
        }
        
        // stateManager.getState(GameState.class).buildSomething();
    }
    
    private void setupBuildIcons(){
        /*
        new ImageBuilder("buildRooms"){{
            filename("Images/build_room.png");
            alignCenter();
            height("95%");
            width("100%h");
            interactOnClick("onBuildRoom()");
        }}.build(nifty, screen, thirdRowTop);
        */
        new ImageBuilder("adamRoom"){{
            filename("Images/build_path.png");
            height("95%");
            width("100%h");
            alignCenter();
            interactOnClick("onBuildPath()");   
                }}.build(nifty, screen, firstRowTop);
        
        new ImageBuilder("adamRoom"){{
            filename("Images/adam_room.png");
            height("95%");
            width("100%h");
            alignCenter();
            interactOnClick("buildAdamMenu()");   
                }}.build(nifty, screen, thirdRowTop);
        
        new ImageBuilder("kythosRoom"){{
            filename("Images/kythos_room.png");
            height("95%");
            width("100%h");
            alignCenter();
            interactOnClick("buildKythosMenu()");   
                }}.build(nifty, screen, thirdRowTop);
        
        new ImageBuilder("maraRoom"){{
            filename("Images/mara_room.png");
            height("95%");
            width("100%h");
            alignCenter();
            interactOnClick("buildMaraMenu()");   
                }}.build(nifty, screen, thirdRowTop);
    }
    
    public void onArmy(){
        
    }
    
    public void onOptions(){
        System.out.println("InGameInputs: onOptions()");
        stateManager.getState(GameState.class).setEnabled(false);
        stateManager.getState(InGameInputs.class).setEnabled(false);
        stateManager.getState(PauseState.class).setEnabled(true);
        //stateManager.detach(stateManager.getState(GameState.class));
        //stateManager.attach(new PauseState(stateManager, app));
    }
    
    public void onBuildPath(){
        System.out.println("Build a fucking path to get out of here");
    }
    
    public void onBuildRoom(String whichRoom){
        System.out.println("onBuildRoom()");
        
        switch(Integer.parseInt(whichRoom)){
            case ADAM:
                // Adam-Raum anlegen
                System.out.println("Build Adam Room");
                break;
            case KYTHOS:
                // Kythos-Raum anlegen
                System.out.println("Build Kythos Room");
                break;
            case MARA:
                // Mara-Raum anlegen
                System.out.println("Build Mara Room");
                break;
        }
        /*
        switch(menuState){
            case MENU_CLEAR:
                setupRoomIcons();
                menuState = BUILD_ROOMS;
                return;
            case BUILD_ROOMS:
                clearSecondRow();
                menuState = MENU_CLEAR;
                return;
            default:
                clearSecondRow();
                setupRoomIcons();
                menuState = BUILD_ROOMS;
        }
         * */
    }
    
    public void buildAdamRoom(){
        if(adamRooms < 3){
            adamRooms++;
            markNewRoom(ADAM);
        }
            
    }
    
    public void buildKythosRoom(){
        if(kythosRooms < 3){
            kythosRooms++;
            markNewRoom(KYTHOS);
        }
            
    }
    
    public void buildMaraRoom(){
        if(maraRooms < 3){
            maraRooms++;
            markNewRoom(MARA);
        }
            
    }
    
    private void markNewRoom(int whichRoom){
        switch(whichRoom){
            case ADAM:
                Element adamImg = nifty.getScreen("inGameInputs").findElementByName("adamRooms");
                if(adamImg == null){
                    new ImageBuilder("adamRooms"){{
                    filename("Images/adam_number_" + adamRooms + ".png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildAdamMenu())");   
                        }}.build(nifty, screen, thirdRowTop);
                }else{
                    NiftyImage img = nifty.getRenderEngine().createImage(
                            "Images/adam_number_" + adamRooms + ".png", false);
                    adamImg.getRenderer(ImageRenderer.class).setImage(img);
                }
                
                break;
            case KYTHOS:
                Element kythosImg = nifty.getScreen("inGameInputs").findElementByName("kythosRooms");
                if(kythosImg == null){
                    new ImageBuilder("kythosRooms"){{
                    filename("Images/kythos_number_" + kythosRooms + ".png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildKythosMenu()");   
                        }}.build(nifty, screen, thirdRowTop);
                }else{
                    NiftyImage img = nifty.getRenderEngine().createImage(
                            "Images/kythos_number_" + kythosRooms + ".png", false);
                    kythosImg.getRenderer(ImageRenderer.class).setImage(img);
                }
                
                break;
            case MARA:
                Element maraImg = nifty.getScreen("inGameInputs").findElementByName("maraRooms");
                if(maraImg == null){
                    new ImageBuilder("maraRooms"){{
                    filename("Images/mara_number_" + maraRooms + ".png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildMaraMenu())");   
                        }}.build(nifty, screen, thirdRowTop);
                }else{
                    NiftyImage img = nifty.getRenderEngine().createImage(
                            "Images/mara_number_" +  maraRooms + ".png", false);
                    maraImg.getRenderer(ImageRenderer.class).setImage(img);
                }
                
                break;
            default:
                return;
        }
        
        menuThirdRow.layoutElements();
    }
    
    public void buildAdamMenu(){
        switch(menuState){
            case MENU_CLEAR:
                setupRoomName(ADAM);
                setupBuildingIcons(ADAM);
                menuState = BUILD_ADAM;
                return;
            case BUILD_ADAM:
                clearText();
                clearSecondRow();
                menuState = MENU_CLEAR;
                return;
            default:
                clearText();
                clearSecondRow();
                setupRoomName(ADAM);
                setupBuildingIcons(ADAM);
                menuState = BUILD_ADAM;
        }
    }
    
    public void buildKythosMenu(){
        switch(menuState){
            case MENU_CLEAR:
                setupRoomName(KYTHOS);
                setupBuildingIcons(KYTHOS);
                menuState = BUILD_KYTHOS;
                return;
            case BUILD_KYTHOS:
                clearText();
                clearSecondRow();
                menuState = MENU_CLEAR;
                return;
            default:
                clearSecondRow();
                clearText();
                setupRoomName(KYTHOS);
                setupBuildingIcons(KYTHOS);
                menuState = BUILD_KYTHOS;
        }
    }
    
    public void buildMaraMenu(){
        switch(menuState){
            case MENU_CLEAR:
                setupRoomName(MARA);
                setupBuildingIcons(MARA);
                menuState = BUILD_MARA;
                return;
            case BUILD_MARA:
                clearText();
                clearSecondRow();
                menuState = MENU_CLEAR;
                return;
            default:
                clearText();
                clearSecondRow();
                setupRoomName(MARA);
                setupBuildingIcons(MARA);
                menuState = BUILD_MARA;
        }
    }
    
    private void setupRoomName(int whichRoom){
        Element text = screen.findElementByName("roomName");
        
        switch(whichRoom){
            case ADAM:
                text.getRenderer(TextRenderer.class).setText("Adam Raum");
                break;
            case KYTHOS:
                text.getRenderer(TextRenderer.class).setText("Kythos Raum");
                break;
            case MARA:
                text.getRenderer(TextRenderer.class).setText("Mara Raum");
                break;
        }
    }
    
    private void clearText(){
        Element text = screen.findElementByName("roomName");
        text.getRenderer(TextRenderer.class).setText("");
    }
    
    private void setupBuildingIcons(int whichBuilding){
    final int whichRoom = whichBuilding;        
    
        new ImageBuilder("buildRoom"){{
            filename("Images/build_room.png");
            alignCenter();
            height("95%");
            width("100%h");
            interactOnClick("onBuildRoom(" + whichRoom + ")");
        }}.build(nifty, screen, secondRowTop);
        
        switch(whichBuilding){
            case ADAM:
                new ImageBuilder("buildAdamOne"){{
                    filename("Images/adam_1.png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildAdamBuilding(1)");   
                        }}.build(nifty, screen, secondRowTop);
                new ImageBuilder("buildAdamTwo"){{
                    filename("Images/adam_2.png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildAdamBuilding(2)");   
                        }}.build(nifty, screen, secondRowTop);
                new ImageBuilder("buildAdamThree"){{
                    filename("Images/adam_3.png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildAdamBuilding(3)");   
                        }}.build(nifty, screen, secondRowTop);
                return;
            case KYTHOS:
               new ImageBuilder("buildKythosOne"){{
                    filename("Images/kythos_1.png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildKythosBuilding(1)");   
                        }}.build(nifty, screen, secondRowTop);
                new ImageBuilder("buildKythosTwo"){{
                    filename("Images/kythos_2.png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildKythosBuilding(2)");   
                        }}.build(nifty, screen, secondRowTop);
                new ImageBuilder("buildKythosThree"){{
                    filename("Images/kythos_3.png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildKythosBuilding(3)");   
                        }}.build(nifty, screen, secondRowTop);
                return;
            case MARA:
                new ImageBuilder("buildMaraOne"){{
                    filename("Images/mara_1.png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildMaraBuilding(1)");   
                        }}.build(nifty, screen, secondRowTop);
                new ImageBuilder("buildMaraTwo"){{
                    filename("Images/mara_2.png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildMaraBuilding(2)");   
                        }}.build(nifty, screen, secondRowTop);
                new ImageBuilder("buildMaraThree"){{
                    filename("Images/mara_3.png");
                    height("95%");
                    width("100%h");
                    alignCenter();
                    interactOnClick("buildMaraBuilding(3)");   
                        }}.build(nifty, screen, secondRowTop);
                return;
        }
    }
    
    public void buildAdamBuilding(String whichLevel){
        switch(Integer.parseInt(whichLevel)){
            case BUILDING_LEVEL_ONE:
                // Hier Adam-Building Stufe 1
                updateRessources(ADAM, BUILDING_LEVEL_ONE);
                return;
            case BUILDING_LEVEL_TWO:
                // Hier Adam-Building Stufe 2
                updateRessources(ADAM, BUILDING_LEVEL_TWO);
                return;
            case BUILDING_LEVEL_THREE:
                // Hier Adam-Building Stufe 3
                updateRessources(ADAM, BUILDING_LEVEL_THREE);
                return;
        }
    }
    
    public void buildKythosBuilding(String whichLevel){
        switch(Integer.parseInt(whichLevel)){
            case BUILDING_LEVEL_ONE:
                updateRessources(KYTHOS, BUILDING_LEVEL_ONE);
                return;
            case BUILDING_LEVEL_TWO:
                updateRessources(KYTHOS, BUILDING_LEVEL_TWO);
                return;
            case BUILDING_LEVEL_THREE:
                updateRessources(KYTHOS, BUILDING_LEVEL_THREE);
                return;
        }
    }
    
    public void buildMaraBuilding(String whichLevel){
        switch(Integer.parseInt(whichLevel)){
            case BUILDING_LEVEL_ONE:
                updateRessources(MARA, BUILDING_LEVEL_ONE);
                return;
            case BUILDING_LEVEL_TWO:
                updateRessources(MARA, BUILDING_LEVEL_TWO);
                return;
            case BUILDING_LEVEL_THREE:
                updateRessources(MARA, BUILDING_LEVEL_THREE);
                return;
        }
    }
    
    private void clearMenu(){
       menuState = MENU_CLEAR;
       clearText();
       clearThirdRow();
       clearSecondRow();
       clearFirstRow();
    }
    
    private void clearFirstRow(){        
        List<Element> visibleElements = firstRowTop.getElements();
        
        for(Element element: visibleElements){
            element.markForRemoval();
        }
        
        visibleElements = firstRowBottom.getElements();
        for(Element element: visibleElements){
            element.markForRemoval();
        }
        
        firstRowTop.layoutElements();
        firstRowBottom.layoutElements();
    }
    
    private void clearSecondRow(){
        List<Element> visibleElements = secondRowTop.getElements();
        
        for(Element element: visibleElements){
            element.markForRemoval();
        }
        
        visibleElements = secondRowBottom.getElements();
        for(Element element: visibleElements){
            element.markForRemoval();
        }
        
        secondRowTop.layoutElements();
        secondRowBottom.layoutElements();
    }
    
    private void clearThirdRow(){
        List<Element> visibleElements = thirdRowTop.getElements();
        
        for(Element element: visibleElements){
            System.out.println("clearThirdRow: " + element.getId());
            element.markForRemoval();
        }
        
        visibleElements = thirdRowBottom.getElements();
        for(Element element: visibleElements){
            element.markForRemoval();
        }
        
        thirdRowTop.layoutElements();
        thirdRowBottom.layoutElements();
    }
    
    private void updateRessources(int whichRessource, int whichLevel){
        switch(whichRessource){
            case ADAM:
                if(whichLevel == BUILDING_LEVEL_ONE){
                    PlayerRessources.adamCreatorSMALL++;
                }else if(whichLevel == BUILDING_LEVEL_TWO){
                    PlayerRessources.adamCreatorMIDDLE++;
                }else if(whichLevel == BUILDING_LEVEL_THREE){
                    PlayerRessources.adamCreatorBIG++;
                }
                break;
            case KYTHOS:
                if(whichLevel == BUILDING_LEVEL_ONE){
                    PlayerRessources.kythosCreatorSMALL++;
                }else if(whichLevel == BUILDING_LEVEL_TWO){
                    PlayerRessources.kythosCreatorMIDDLE++;
                }else if(whichLevel == BUILDING_LEVEL_THREE){
                    PlayerRessources.kythosCreatorBIG++;
                }
                break;
            case MARA:
                if(whichLevel == BUILDING_LEVEL_ONE){
                    PlayerRessources.maraCreatorSMALL++;
                }else if(whichLevel == BUILDING_LEVEL_TWO){
                    PlayerRessources.maraCreatorMIDDLE++;
                }else if(whichLevel == BUILDING_LEVEL_THREE){
                    PlayerRessources.maraCreatorBIG++;
                }
                break;
        }
    }
    
    public void ressourcesChanged(){
        Element adam = nifty.getScreen("inGameInputs").findElementByName("adamLabel");
        Element kythos = nifty.getScreen("inGameInputs").findElementByName("kythosLabel");
        Element mara = nifty.getScreen("inGameInputs").findElementByName("maraLabel");
        Element darkness = nifty.getScreen("inGameInputs").findElementByName("darknessLabel");
        adam.getRenderer(TextRenderer.class).setText(String.valueOf(PlayerRessources.adam));
        kythos.getRenderer(TextRenderer.class).setText(String.valueOf(PlayerRessources.kythos));
        mara.getRenderer(TextRenderer.class).setText(String.valueOf(PlayerRessources.mara));
        darkness.getRenderer(TextRenderer.class).setText(String.valueOf(PlayerRessources.darkness));
    }
    
    private void createOptionsMenu(){
        optionsMenu = nifty.createPopup("niftyPopupMenu");
        Menu myMenu = optionsMenu.findNiftyControl("#menu", Menu.class);
        myMenu.setWidth(new SizeValue("400px"));
        myMenu.addMenuItem("Click me!", "Images/quit.png", 
            new menuItem("quit", "Quit")); 
        nifty.subscribe(
            nifty.getScreen("inGameInputs"), 
            myMenu.getId(), 
            MenuItemActivatedEvent.class, 
            new MenuItemActivatedEventSubscriber());
    }
    
    public void closePopup(){
        
    }
    
    public void onDoNothing(){
        
    }
    
    private class menuItem {
        public String id;
        public String name;

        public menuItem(String id, String name){
               this.id= id;
               this.name = name;
        }
    }
    
    private class MenuItemActivatedEventSubscriber 
        implements EventTopicSubscriber<MenuItemActivatedEvent> {
 
        @Override
        public void onEvent(final String id, final MenuItemActivatedEvent event) {
            menuItem item = (menuItem) event.getItem();
            
            if ("quit".equals(item.id)) {
                System.out.println("Quit Game");
            }
        }
  };
}
